﻿Ext.define('NicksList.controller.Main', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            listList: '#listList',
            itemList: '[itemId="itemList"]',
            panelListBrowser: '#panelListBrowser',
            btnAddItem: 'button[itemId="btnAddItem"]',
            btnAddList: 'button[itemId="btnAddList"]',
            btnSubmitList: 'button[itemId="btnSubmitList"]',
            btnSubmitItem: 'button[itemId="btnSubmitItem"]'
        },
        control: {
            listList: {
                itemtap: 'onListListTap',
            },
            itemList: {
                itemtap: 'onItemListTap',
            },
            btnAddList: {
                tap: 'showAddNewList'
            },
            btnAddItem: {
                tap: 'showAddNewItem'
            },
            btnSubmitList: {
                tap: 'submitNewList'
            },
            btnSubmitItem: {
                tap: 'submitNewItem'
            }
        }
    },
    onListListTap: function (dataview, index, target, record, e, eOpts) {
        var store = Ext.getStore('ListStore');
        if (e.target.className == 'delete-btn') {
            store.remove(record);
        } else {
            this.getPanelListBrowser().push(Ext.create('NicksList.view.List', {
                id: 'list2',
                listeners: {
                    show: function () {
                        var itemStore = Ext.getStore('ItemStore');
                        itemStore.clearFilter();
                        itemStore.filterBy(function (record2, id) {
                            return record2.data.ListID == record.data.ListID;
                        });
                    }
                }
            }));
            this.currentListID = record.data.ListID;
        }
    },
    onItemListTap: function (dataview, index, target, record, e, eOpts) {
        if (e.target.className == 'delete-btn') {
            var store = Ext.getStore('ItemStore');
            store.remove(record);
        }
    },
    showAddNewList: function () {
        this.getPanelListBrowser().push(Ext.create('NicksList.view.AddListForm', {
            itemId: 'frmAddNewList',
            id: 'frmAddNewList'
        }));
    },
    showAddNewItem: function () {
        this.getPanelListBrowser().push(Ext.create('NicksList.view.AddItemForm', {
            itemId: 'frmAddNewItem',
            id: 'list4'
        }));
    },
    submitNewList: function () {
        var frmAddNewList = Ext.ComponentQuery.query('#frmAddNewList')[0];
        var store = Ext.getStore('ListStore');
        var formValues = frmAddNewList.getValues();
        var newList = Ext.create('NicksList.model.MList', {
            ListID: store.data.length,
            Name: formValues.ListName
        });
        store.add(newList);
        this.getPanelListBrowser().pop();
    },
    submitNewItem: function () {
        var frmAddNewItem = Ext.ComponentQuery.query('#frmAddNewItem')[0];
        var store = Ext.getStore('ItemStore');
        var formValues = frmAddNewItem.getValues();
        var newList = Ext.create('NicksList.model.MItem', {
            ListID: this.currentListID,
            ItemID: store.data.length,
            Name: formValues.ItemName
        });
        store.add(newList);
        this.getPanelListBrowser().pop();
    }
});